package com.logisticsrepair.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.logisticsrepair.entity.RepairOrdersEntity;
import com.logisticsrepair.enums.RepairStatusEnum;
import com.logisticsrepair.service.RepairOrdersService;
import com.logisticsrepair.mapper.RepairOrdersMapper;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author huahua
 * @description 针对表【repair_orders(报修订单)】的数据库操作Service实现
 * @createDate 2022-03-12 20:10:16
 */
@Service
public class RepairOrdersServiceImpl extends ServiceImpl<RepairOrdersMapper, RepairOrdersEntity>
        implements RepairOrdersService {

    @Autowired
    private RepairOrdersMapper repairOrdersMapper;

    /**
     * 查询状态为待受理（1）的订单信息
     *
     * @return
     */
    @Override
    public List<RepairOrdersEntity> queryNewOrders() {
        return repairOrdersMapper.queryNewOrders();
    }

    /**
     * 根据用户唯一标识，查询用户所有订单记录
     *
     * @param userOpenid 用户唯一标识
     * @return
     */
    @Override
    public List<RepairOrdersEntity> queryUserOrderRecode(String userOpenid) {
        return repairOrdersMapper.queryUserOrderRecode(userOpenid);
    }

    /**
     * 维修员根据订单编号接单，并依据版本号更新维修状态等信息
     *
     * @param repairVersion 版本号-乐观锁
     * @param workerOpenid  维修员唯一标识
     * @param orderId       订单编号
     * @return [更新失败：0，更新成功：1，更新冲突：-1]
     */
    @Override
    public int acceptOrder(Integer repairVersion, String workerOpenid, String orderId) {
        RepairOrdersEntity repairOrdersEntity = new RepairOrdersEntity();
        repairOrdersEntity.setOrderId(orderId);
        repairOrdersEntity.setStatusCode(RepairStatusEnum.HAVE_ACCEPTED);
        repairOrdersEntity.setWorkerOpenid(workerOpenid);
        repairOrdersEntity.setRepairVersion(repairVersion);
        try {
            // 更新失败：0，更新成功：1
            return Boolean.compare(this.updateById(repairOrdersEntity), Boolean.TRUE) + 1;
        } catch (MyBatisSystemException e) {
            e.printStackTrace();
            //更新错误
            return -1;
        }
    }

    /**
     * 查询维修订单
     * @param condition
     * @return 订单对象list
     */
    @Override
    public List<RepairOrdersEntity> queryWorkerOrder(Map<String, Object> condition) {
        return repairOrdersMapper.queryWorkerOrder(condition);
    }

    /**
     * 驳回订单
     *
     * @param repairOrdersEntity
     * @return
     */
    @Override
    public boolean rejectOrder(RepairOrdersEntity repairOrdersEntity) {
        LambdaUpdateWrapper<RepairOrdersEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(RepairOrdersEntity::getOrderId, repairOrdersEntity.getOrderId());

        RepairOrdersEntity entity = new RepairOrdersEntity();
        entity.setStatusCode(RepairStatusEnum.REJECTED);
        entity.setRejectReason(repairOrdersEntity.getRejectReason());
        entity.setRepairVersion(repairOrdersEntity.getRepairVersion());

        return this.update(entity, lambdaUpdateWrapper);
    }
}




